package com.ooqiu.gaming.ui.register;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.base.BaseActivity;
import com.ooqiu.gaming.common.Constants;
import com.ooqiu.gaming.components.retrofit.progress.ProgressSubscriber;
import com.ooqiu.gaming.ui.login.LoginActivity;
import com.ooqiu.gaming.utils.RxCountDown;
import com.ooqiu.gaming.view.SquareImageView;
import com.ooqiu.gaming.view.ToastUtil;
import com.ooqiu.gaming.view.Topbar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

@Route(path = RegisterActivity.PATH)
public class RegisterActivity extends BaseActivity {
    public static final String PATH = "/gn/register";
    @BindView(R.id.topbar)
    Topbar mTopbar;
    @BindView(R.id.edit_mobile)
    EditText mEditMobile;
    @BindView(R.id.edit_pwd)
    EditText mEditPwd;
    @BindView(R.id.edit_phone_code)
    EditText mEditPhoneCode;
    @BindView(R.id.btn_register)
    Button mBtnRegister;

    GamingApi mGamingApi;
    @BindView(R.id.img_visibility)
    SquareImageView mImgVisibility;
    @BindView(R.id.tv_countdown)
    TextView mTvCountdown;

    @Override
    public int initContentView() {
        return R.layout.activity_register;
    }

    @Override
    public void initInjector() {
        ButterKnife.bind(this);
        mGamingApi = getApplicationComponent().getGamingApi();
    }

    @Override
    public void initUiAndListener() {
        mTopbar.setOnNavBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getRouter().build(LoginActivity.PATH)
                        .navigation();
                finish();
            }
        });


        mTopbar.setOnMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    protected Toolbar toolbarIdRes() {
        return mTopbar;
    }

    @Override
    public Drawable getStateBarDrawable() {
        return new ColorDrawable(Color.WHITE);
    }

    @Override
    protected View statusLayoutId() {
        return null;
    }

    @OnClick(R.id.btn_register)
    public void onClick() {
        if (TextUtils.isEmpty(mEditMobile.getText())) {
            ToastUtil.showWarning("手机号不能为空");
            return;
        }

        if (TextUtils.isEmpty(mEditPwd.getText())) {
            ToastUtil.showWarning( "密码不能为空");

            return;
        }

        if (TextUtils.isEmpty(mEditPhoneCode.getText())) {
            ToastUtil.showWarning( "验证码不能为空");

            return;
        }

        register(mEditMobile.getText().toString(), mEditPwd.getText().toString(), mEditPhoneCode.getText().toString());


    }

    @OnClick({R.id.img_visibility, R.id.tv_countdown})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_visibility:
                if (mImgVisibility.isSelected()) {
                    mImgVisibility.setSelected(false);
                    mEditPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    mImgVisibility.setSelected(true);
                    mEditPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.tv_countdown:
                if (!TextUtils.isEmpty(mEditMobile.getText())) {
                    fetchCode(mEditMobile.getText().toString());
                } else {
                    ToastUtil.showWarning("手机号不能为空");
                }
                break;
        }
    }


    private void register(final String mobile, String pwd, String authCode) {
        mGamingApi.register(mobile, pwd, authCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<String>(mContext) {
                    @Override
                    public void onNext(String object) {
                        ARouter.getInstance().build(LoginActivity.PATH)
                                .withString(Constants.ROUTER_KEY_MOBILE, mobile)
                                .navigation();
                        finish();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    private void fetchCode(String mobile) {
        mGamingApi.fetchCode(mobile, 1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<String>(mContext) {
                    @Override
                    public void onNext(String object) {
                        countdown();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    private void countdown() {
        RxCountDown.countdown(60)
                .subscribe(new DefaultObserver<Integer>() {
                    @Override
                    protected void onStart() {
                        mTvCountdown.setEnabled(false);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        mTvCountdown.setText(String.valueOf(integer));
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        mTvCountdown.setText("获取");
                        mTvCountdown.setEnabled(true);
                    }

                });
    }
}
